Shader "XC/WarFogBlit"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "black" {}
        _MaskTex ("MaskTex", 2D) = "black" {}
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }
        LOD 100

        Pass
        {
            HLSLPROGRAM
            #pragma target 3.5
            #pragma vertex vert
            #pragma fragment frag
   
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "WarFogUtil.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            TEXTURE2D_FLOAT(_MainTex);
            SAMPLER(sampler_MainTex);
            TEXTURE2D(_MaskTex);
            SAMPLER(sampler_MaskTex);
            CBUFFER_START(UnityPerMaterial) 
            float4 _MainTex_ST;           
            float _Step;
            float4 uvs[512];
            CBUFFER_END

            

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = TransformObjectToHClip(v.vertex.xyz);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                // sample the texture
               float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
               uint id = decodeId(col);
               float4 uv=uvs[id];
               float2 uv_start=uv.xy;
               uint2 uv_int=(uint2)uv_start;
               uv_start-=uv_int;
               uv_int>>=1;
               int fogClear=uv_int.y!=0;
               float _step=_Step;
               float2 uv_offset=uv.zw-uv_start;  
               float2 s_uv=(i.uv-uv_start)/uv_offset;
               float4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex,s_uv);
               _step+=((1-fogClear)*(1-mask.a)*0.1f+fogClear*mask.a*0.5);
               _step*=(2*fogClear-1);     
               col.b = col.b+_step;
               return col;
            }
            ENDHLSL
        }
    }
}